﻿function UserLogin() {
    $('#aspnetForm').submit();
}

function FromPost() {
    var errText = $("#errText");
    errText[0].className = "errText";
    SetAjaxLoad(errText[0], "正在登陆中...");

    var adminname = $("#username");
    var password = $("#password");

    if (adminname.val() == "") {
        errText.html("请输入用户名！");
        errText[0].className = "errTextW";
        adminname.focus();
        return false;
    }

    if (password.val() == "") {
        errText.html("请输入登陆密码！");
        errText[0].className = "errTextW";
        password.focus();
        return false;
    }
    return true;
}

//$(document).ready(function () {
//    var options;
//    options = {
//        beforeSubmit: FromPost,
//        dataType: 'json',
//        success: UserLoginSuccess,
//        error: loginError
//    };
//    $("#aspnetForm").ajaxForm(options);
//});

function UserLoginSuccess(data) {
    if (data.state) {
        if (data.callback != null) {
            if (data.callback.indexOf("(") > -1) {
                try { eval(data.callback); } catch (err) { }
                return;
            } else {
                try { eval(data.callback + "(\"" + data.message + "\")"); } catch (err) { /*alert(err)*/ }
                return;
            }
        }
        SetAjaxDiv("ok", false, data.message);
        var func = AfterLoginSuccess(data.data);
        setTimeout(func, 1000);
    } else {
        SetAjaxDiv("err", false, data.message);
    }
}

function AfterLoginSuccess(data) {
    return function () {
        var ulcontent = "<ul>";
        ulcontent += "<li>用户名：</li>";
        ulcontent += "<li>级 别：<img src='../images/lv2.png' width='35' height='11' align='absmiddle' />''童生</li>";
        ulcontent += "<li>铜 钱：<b>164</b> 文</li>";
        ulcontent += "<li><a href=\"/User/UserInfo.aspx\" class=\"l256ebe\">用户中心</a> <a href=\"/Shopping/ShoppingCart.aspx\" class=\"l256ebe\">我的购物车</a> <a href=\"#\">退出</a></li>";
        ulcontent += "</ul>";

        $("#divUser").html(ulcontent);
    }
}

function EditShoppingCart(ntotal) {
    $("#sCart").html("(" + ntotal + ")");
}

function loginError(data)
{
    alert(data);
}

function SetAjaxLoad(obj, txt) {
    obj.innerHTML = "<table width=\"98%\" height=\"16\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
	  			+ "<tr>"
	    + "<td width='1%'><img src='images/ajax-loadersmall.gif' /></td>"
        + "<td width='99%' align=\"left\" class=\"lodertxt\">&nbsp;&nbsp;" + txt + "</tr>"
	+ "</table>";
}